package com.ynqxg.router

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import com.ynqxg.router.manager.RouterManager
import com.ynqxg.router.processor.Router
import com.ynqxg.router.view.SmartTextView


@Router("main")
class MainActivity : AppCompatActivity() {

    private val textView by lazy { findViewById<SmartTextView>(R.id.tv) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val id = intent.getIntExtra("id", 1)
        textView.setOnClickListener { _ ->
            goNextPage(id)
        }
    }

    private fun goNextPage(id: Int) {
        RouterManager(this).routerTo("main", Intent().also {
            it.putExtra("id", id + 1)
        })
        finishAfterTransition()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_DOWN) {
            if (supportFragmentManager.backStackEntryCount > 0) {
                supportFragmentManager.popBackStack()
            } else {
                finishAfterTransition()
            }
            return true
        }
        finishAfterTransition()
        return super.onKeyDown(keyCode, event)
    }

}